<?php
/**
 * 用户
 * auth WQl
 * 2021/6/16 18:09
 */

//用户列表
$router->get("/admin/user/index",function (Req $req){
        $page   = $req->param("page",1);
        $limit  = $req->param("limit",10);
        return $req->success(\service\UserS::getInstance()->index($page,$limit));
});

//添加用户
$router->post("/admin/user/add",function (Req $req){
        $info       = $req->param("info",false);
        $realName   = $req->param("real_name",false);
        $role       = $req->param("role",false);
        $phone      = $req->param("phone",false);
        if(!$req->checkPhone($phone)) return $req->no("手机号不合法");
        if(!$role) return $req->no("请选择身份");
        if(!$info) return $req->no("请输入信息");
        if(!$realName) return $req->no("真实姓名必填");
        try{
            \service\UserS::getInstance()->add($info,$realName,$role,$phone,$req->getUserId());
            return $req->success();
        } catch (Throwable $exception) {
            return $req->no($exception->getMessage());
        }
},"*");

//用户登录
$router->post("/admin/user/login",function (Req $req){
    $pwd = $req->param("pwd",false);
    $info = $req->param("info",false);
    $code = $req->param("code",false);
    if(!$pwd || !$info || !$code) return $req->no("用户名、密码、验证码不能为空");
    try{
        $token = \service\UserS::getInstance()->login($info,$pwd,$code,$req);
        return $req->success($token);
    } catch (ApiError $exception) {
        return $req->no($exception->getError());
    } catch (Throwable $exception) {
    }   return $req->no($exception->getMessage());
},false);

//获取用户登录验证码
$router->post("/admin/user/login_code",function (Req $req){
    $pwd = $req->param("pwd",false);
    $info = $req->param("info",false);
    if(!$pwd || !$info) return $req->no("用户名密码不能为空");
    try{
        \service\UserS::getInstance()->loginCode($info,$pwd,$req);
        return $req->success();
    } catch (ApiError $exception) {
        return $req->no($exception->getError());
    } catch (Throwable $exception) {
        return $req->no([$exception->getMessage(),$exception->getFile(),$exception->getLine()]);
    }
},false);

//获取自己的信息
$router->get("/user/info",function (Req $req){
    try{
        $info = \service\UserS::getInstance()->info($req->getUserId());
        return $req->success($info);
    } catch (Throwable $exception) {
        return $req->no($exception->getMessage());
    }
});

//修改用户身份权限
$router->post("/user/role",function (Req $req){
    try{
        \service\UserS::getInstance()->editRole($req);
        return $req->success();
    } catch (Throwable $exception) {
        return $req->no($exception->getMessage());
    }
},[1]);